package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.oss.utils.ServletUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.WebAdviceEntity;
import io.renren.modules.generator.service.WebAdviceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 首页留言表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-24 12:56:48
 */
@RestController
@RequestMapping("generator/webadvice")
@Api(value = "首页留言",tags = {"首页留言"})
public class WebAdviceController {
    @Autowired
    private WebAdviceService webAdviceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:webadvice:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = webAdviceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{adviceId}")
    @RequiresPermissions("generator:webadvice:info")
    public R info(@PathVariable("adviceId") Integer adviceId){
		WebAdviceEntity webAdvice = webAdviceService.getById(adviceId);

        return R.ok().put("webAdvice", webAdvice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:webadvice:save")
    public R save(@RequestBody WebAdviceEntity webAdvice){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long userId = userEntity.getUserId();
//        webAdvice.setCreateUser(Math.toIntExact(userId));
        HttpServletRequest request = ServletUtils.getRequest();
        String remoteHost = request.getRemoteHost();
        webAdvice.setAdviceIp(remoteHost);
        webAdvice.setAdviceStatus(0);
        webAdvice.setCreateTime(new Date());
        webAdvice.setUpdateTime(new Date());
		webAdviceService.save(webAdvice);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:webadvice:update")
    public R update(@RequestBody WebAdviceEntity webAdvice){
		webAdviceService.updateById(webAdvice);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:webadvice:delete")
    public R delete(@RequestBody Integer[] adviceIds){
		webAdviceService.removeByIds(Arrays.asList(adviceIds));

        return R.ok();
    }

}
